<script setup lang="ts">
import { computed, ref, watch } from 'vue';

import { message } from 'ant-design-vue';

interface Props {
  visible: boolean;
  record?: any;
}

interface Emits {
  (e: 'update:visible',value: boolean): void;
  (e: 'success',items: any[]): void
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 响应式数据
const selectedCategory = ref('injection');
const selectedItems = ref<string[]>([]);
const searchQuery = ref('');
const treatmentDate = ref(null);
const treatmentTime = ref(null);
const nurse = ref('');
const treatmentRoom = ref('');
const treatmentNotes = ref('');

// 处置项目数据
const treatmentItems = ref([
  // 注射处置
  {
    id: 'T001',
    category: 'injection',
    name: '肌肉注射',
    price: 20,
    description: '肌肉注射药物',
    duration: '10分钟',
  },
  {
    id: 'T002',
    category: 'injection',
    name: '皮下注射',
    price: 15,
    description: '皮下注射药物',
    duration: '8分钟',
  },
  {
    id: 'T003',
    category: 'injection',
    name: '静脉注射',
    price: 30,
    description: '静脉注射药物',
    duration: '15分钟',
  },

  // 输液处置
  {
    id: 'T004',
    category: 'infusion',
    name: '静脉输液',
    price: 50,
    description: '静脉输液治疗',
    duration: '1-2小时',
  },
  {
    id: 'T005',
    category: 'infusion',
    name: '营养液输注',
    price: 80,
    description: '营养液静脉输注',
    duration: '2-3小时',
  },
  {
    id: 'T006',
    category: 'infusion',
    name: '输血',
    price: 200,
    description: '血液输注',
    duration: '2-4小时',
  },

  // 换药处置
  {
    id: 'T007',
    category: 'dressing',
    name: '伤口换药',
    price: 25,
    description: '伤口清洁换药',
    duration: '20分钟',
  },
  {
    id: 'T008',
    category: 'dressing',
    name: '拆线',
    price: 30,
    description: '手术伤口拆线',
    duration: '15分钟',
  },
  {
    id: 'T009',
    category: 'dressing',
    name: '包扎固定',
    price: 35,
    description: '外伤包扎固定',
    duration: '25分钟',
  },

  // 物理治疗
  {
    id: 'T010',
    category: 'physical',
    name: '理疗',
    price: 60,
    description: '物理治疗',
    duration: '30分钟',
  },
  {
    id: 'T011',
    category: 'physical',
    name: '按摩',
    price: 40,
    description: '康复按摩',
    duration: '20分钟',
  },
  {
    id: 'T012',
    category: 'physical',
    name: '热敷',
    price: 20,
    description: '热敷治疗',
    duration: '15分钟',
  },

  // 其他处置
  {
    id: 'T013',
    category: 'other',
    name: '导尿',
    price: 45,
    description: '导尿管插入',
    duration: '20分钟',
  },
  {
    id: 'T014',
    category: 'other',
    name: '灌肠',
    price: 35,
    description: '灌肠处置',
    duration: '30分钟',
  },
  {
    id: 'T015',
    category: 'other',
    name: '采血',
    price: 15,
    description: '血液采集',
    duration: '10分钟',
  },
]);

// 表格列定义
const selectedColumns = [
  {
    title: '处置名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    width: 100,
  },
  {
    title: '预计时长',
    dataIndex: 'duration',
    key: 'duration',
    width: 120,
  },
  {
    title: '操作',
    key: 'action',
    width: 80,
  },
];

// 计算属性
const filteredItems = computed(() => {
  let result = treatmentItems.value.filter(
    (item) => item.category === selectedCategory.value,
  );

  if (searchQuery.value) {
    const search = searchQuery.value.toLowerCase();
    result = result.filter(
      (item) =>
        item.name.toLowerCase().includes(search) ||
        item.description.toLowerCase().includes(search),
    );
  }

  return result;
});

const selectedItemsData = computed(() => {
  return treatmentItems.value.filter((item) =>
    selectedItems.value.includes(item.id),
  );
});

const totalAmount = computed(() => {
  return selectedItemsData.value.reduce((total, item) => total + item.price, 0);
});

// 方法
const onCategoryChange = () => {
  selectedItems.value = [];
};

const searchTreatment = () => {
  // 搜索逻辑已在计算属性中实现
};

const toggleItem = (itemId: string) => {
  const index = selectedItems.value.indexOf(itemId);
  if (index === -1) {
    selectedItems.value.push(itemId);
  } else {
    selectedItems.value.splice(index, 1);
  }
};

const removeItem = (itemId: string) => {
  const index = selectedItems.value.indexOf(itemId);
  if (index !== -1) {
    selectedItems.value.splice(index, 1);
  }
};

const handleOk = () => {
  if (selectedItems.value.length === 0) {
    message.warning('请至少选择一个处置项目');
    return;
  }

  if (!treatmentDate.value) {
    message.warning('请选择处置日期');
    return;
  }

  if (!nurse.value) {
    message.warning('请选择执行护士');
    return;
  }

  // 获取选中的项目数据
  const selectedItemsData = treatmentItems.value.filter((item) =>
    selectedItems.value.includes(item.id),
  );

  message.success('处置项目添加成功');
  emit('success', selectedItemsData);
  emit('update:visible', false);

  // 重置选择
  resetForm();
};

const handleCancel = () => {
  emit('update:visible', false);
  resetForm();
};

const resetForm = () => {
  selectedItems.value = [];
  searchQuery.value = '';
  treatmentDate.value = null;
  treatmentTime.value = null;
  nurse.value = '';
  treatmentRoom.value = '';
  treatmentNotes.value = '';
};

// 监听模态框关闭
watch(
  () => props.visible,
  (visible) => {
    if (!visible) {
      resetForm();
    }
  },
);
</script>

<template>
  <a-modal
    :open="visible"
    title="添加处置项目"
    width="800px"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <div class="treatment-modal">
      <!-- 处置分类选择 -->
      <div class="category-section">
        <h4>处置分类</h4>
        <a-radio-group
          v-model:value="selectedCategory"
          @change="onCategoryChange"
        >
          <a-radio-button value="injection">注射处置</a-radio-button>
          <a-radio-button value="infusion">输液处置</a-radio-button>
          <a-radio-button value="dressing">换药处置</a-radio-button>
          <a-radio-button value="physical">物理治疗</a-radio-button>
          <a-radio-button value="other">其他处置</a-radio-button>
        </a-radio-group>
      </div>

      <!-- 处置项目列表 -->
      <div class="items-section">
        <h4>处置项目</h4>
        <div class="search-row">
          <a-input
            v-model:value="searchQuery"
            placeholder="搜索处置项目"
            style="width: 300px"
            @keyup.enter="searchTreatment"
          >
            <template #suffix>
              <a-button type="text" @click="searchTreatment">🔍</a-button>
            </template>
          </a-input>
        </div>
        <div class="items-grid">
          <div
            v-for="item in filteredItems"
            :key="item.id"
            class="item-card"
            :class="{ selected: selectedItems.includes(item.id) }"
            @click="toggleItem(item.id)"
          >
            <div class="item-name">{{ item.name }}</div>
            <div class="item-desc">{{ item.description }}</div>
            <div class="item-price">¥{{ item.price.toFixed(2) }}</div>
            <div class="item-duration">预计时长: {{ item.duration }}</div>
          </div>
        </div>
      </div>

      <!-- 已选项目 -->
      <div class="selected-section" v-if="selectedItems.length > 0">
        <h4>已选处置项目</h4>
        <a-table
          :columns="selectedColumns"
          :data-source="selectedItemsData"
          :pagination="false"
          size="small"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'price'">
              ¥{{ record.price.toFixed(2) }}
            </template>
            <template v-else-if="column.key === 'action'">
              <a-button
                type="text"
                danger
                size="small"
                @click="removeItem(record.id)"
              >
                删除
              </a-button>
            </template>
          </template>
        </a-table>

        <div class="total-section">
          <span class="total-label">总计:</span>
          <span class="total-amount">¥{{ totalAmount.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 处置安排 -->
      <div class="schedule-section">
        <h4>处置安排</h4>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="处置日期">
              <a-date-picker
                v-model:value="treatmentDate"
                format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="处置时间">
              <a-time-picker
                v-model:value="treatmentTime"
                format="HH:mm"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="执行护士">
              <a-select
                v-model:value="nurse"
                placeholder="选择执行护士"
                style="width: 100%"
              >
                <a-select-option value="nurse1">王护士</a-select-option>
                <a-select-option value="nurse2">李护士</a-select-option>
                <a-select-option value="nurse3">张护士</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="处置室">
              <a-select
                v-model:value="treatmentRoom"
                placeholder="选择处置室"
                style="width: 100%"
              >
                <a-select-option value="room1">处置室1</a-select-option>
                <a-select-option value="room2">处置室2</a-select-option>
                <a-select-option value="room3">处置室3</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </div>

      <!-- 处置说明 -->
      <div class="notes-section">
        <h4>处置说明</h4>
        <a-textarea
          v-model:value="treatmentNotes"
          placeholder="请输入处置说明..."
          :rows="3"
        />
      </div>
    </div>
  </a-modal>
</template>

<style scoped>
.treatment-modal {
  max-height: 700px;
  overflow-y: auto;
}

.category-section {
  margin-bottom: 24px;
}

.category-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.items-section {
  margin-bottom: 24px;
}

.items-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.search-row {
  margin-bottom: 16px;
}

.items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
  max-height: 300px;
  overflow-y: auto;
}

.item-card {
  padding: 12px;
  cursor: pointer;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  transition: all 0.3s;
}

.item-card:hover {
  border-color: #1890ff;
}

.item-card.selected {
  background: #e6f7ff;
  border-color: #1890ff;
}

.item-name {
  margin-bottom: 4px;
  font-weight: 600;
  color: #262626;
}

.item-desc {
  margin-bottom: 4px;
  font-size: 12px;
  line-height: 1.4;
  color: #666;
}

.item-price {
  margin-bottom: 4px;
  font-size: 14px;
  font-weight: 600;
  color: #f5222d;
}

.item-duration {
  font-size: 12px;
  color: #1890ff;
}

.selected-section {
  padding-top: 16px;
  margin-bottom: 24px;
  border-top: 1px solid #f0f0f0;
}

.selected-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.total-section {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  padding-top: 12px;
  margin-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.total-label {
  font-size: 14px;
  color: #666;
}

.total-amount {
  font-size: 18px;
  font-weight: 700;
  color: #f5222d;
}

.schedule-section {
  margin-bottom: 24px;
}

.schedule-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.notes-section {
  margin-bottom: 16px;
}

.notes-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}
</style>
